package com.woniu.cq.malluser.consumer;

import com.aliyuncs.exceptions.ClientException;
import com.woniu.cq.malluser.common.AliyunMessage;
import com.woniu.cq.malluser.common.RandomUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author Daniel
 * @version 1.0.0
 * @ClassName Myconsumer
 * @Description TODO
 * @createTime 2020年09月24日 14:07:00
 */
@Component                                                                  //只监听code标题的消息
@RocketMQMessageListener(topic = "topic",consumerGroup = "my-consumer",selectorExpression = "code")
public class MyConsumer implements RocketMQListener<String> {
    @Autowired
    private StringRedisTemplate srt;

    @Override
    public void onMessage(String tel) {
        //获取一个随机的验证码
        String code = RandomUtil.randomCode(6);
        //发送短信
        AliyunMessage codeMsg = new AliyunMessage();
        String codeId = null;
        try {
            codeId = codeMsg.sendSms(tel, code);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        //替换掉短信回执的非法字符“^”
        codeId = codeId.replace("^","");
        //存入redis
        srt.opsForHash().put(tel,codeId,code);
        srt.expire(tel,2, TimeUnit.MINUTES);
    }
}
